//
//  NSObject+MGAdd.m
//  MGKit
//
//  Created by yideqiang on 2018/6/15.
//

#import "NSObject+MGAdd.h"

@implementation NSObject (MGAdd)

- (NSInteger)asInteger {
    return [[self asNSNumber] integerValue];
}

- (float)asFloat {
    return [[self asNSNumber] floatValue];
}

- (BOOL)asBool {
    return [[self asNSNumber] boolValue];
}

- (NSNumber *)asNSNumber {
    if ( [self isKindOfClass:[NSNumber class]] )
    {
        return (NSNumber *)self;
    }
    else if ( [self isKindOfClass:[NSString class]] )
    {
        return [NSNumber numberWithFloat:[(NSString *)self floatValue]];
    }
    else if ( [self isKindOfClass:[NSDate class]] )
    {
        return [NSNumber numberWithDouble:[(NSDate *)self timeIntervalSince1970]];
    }
    else if ( [self isKindOfClass:[NSNull class]] )
    {
        return [NSNumber numberWithInteger:0];
    }
    
    return nil;
}

- (NSString *)asNSString {
    if ( [self isKindOfClass:[NSNull class]] )
        return nil;
    
    if ( [self isKindOfClass:[NSString class]] )
    {
        return (NSString *)self;
    }
    else if ( [self isKindOfClass:[NSData class]] )
    {
        NSData * data = (NSData *)self;
        
        NSString * text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        if ( nil == text )
        {
            text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            if ( nil == text )
            {
                text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
            }
        }
        return text;
    }
    else
    {
        return [NSString stringWithFormat:@"%@", self];
    }
}

- (NSDate *)asNSDate {
    if ( [self isKindOfClass:[NSDate class]] )
    {
        return (NSDate *)self;
    }
    else if ( [self isKindOfClass:[NSString class]] )
    {
        NSDate * date = nil;
        
        if ( nil == date )
        {
            static NSDateFormatter * formatter = nil;
            
            if ( nil == formatter )
            {
                formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss z"];
                [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
            }
            
            date = [formatter dateFromString:(NSString *)self];
        }
        
        if ( nil == date )
        {
            static NSDateFormatter * formatter = nil;
            
            if ( nil == formatter )
            {
                formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
                [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
            }
            
            date = [formatter dateFromString:(NSString *)self];
        }
        
        if ( nil == date )
        {
            static NSDateFormatter * formatter = nil;
            
            if ( nil == formatter )
            {
                formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
            }
            
            date = [formatter dateFromString:(NSString *)self];
        }
        
        if ( nil == date )
        {
            static NSDateFormatter * formatter = nil;
            
            if ( nil == formatter )
            {
                formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
                [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
            }
            
            date = [formatter dateFromString:(NSString *)self];
        }
        
        return date;
    }
    else
    {
        return [NSDate dateWithTimeIntervalSince1970:[self asNSNumber].doubleValue];
    }
    
    return nil;
}

- (NSData *)asNSData {
    if ( [self isKindOfClass:[NSString class]] )
    {
        return [(NSString *)self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    }
    else if ( [self isKindOfClass:[NSData class]] )
    {
        return (NSData *)self;
    }
    
    return nil;
}

- (NSArray *)asNSArray {
    if ( [self isKindOfClass:[NSArray class]] )
    {
        return (NSArray *)self;
    }
    else
    {
        return [NSArray arrayWithObject:self];
    }
}

- (NSMutableArray *)asNSMutableArray {
    if ( [self isKindOfClass:[NSMutableArray class]] )
    {
        return (NSMutableArray *)self;
    }
    
    return nil;
}

- (NSDictionary *)asNSDictionary {
    if ( [self isKindOfClass:[NSDictionary class]] )
    {
        return (NSDictionary *)self;
    }
    
    return nil;
}

- (NSMutableDictionary *)asNSMutableDictionary {
    if ([self isKindOfClass:[NSMutableDictionary class]]){
        return (NSMutableDictionary *)self;
    }
    NSDictionary * dict = [self asNSDictionary];
    if ( nil == dict )
        return nil;
    
    return [NSMutableDictionary dictionaryWithDictionary:dict];
}

- (BOOL)isNotNull {
    if ([self isEqual:[NSNull null]]) {
        return NO;
    }
    else if ([self isKindOfClass:[NSNull class]])
    {
        return NO;
    }
    else if (self==nil){
        return NO;
    }
    else {
        if ([self isKindOfClass:[NSString class]]) {
            NSString *str = (NSString *)self;
            if ([str isEqualToString:@""]) {
                return NO;
            }
            return YES;
        }
        else if([self isKindOfClass:[NSArray class]]){
            NSArray *arr = (NSArray *)self;
            if (![arr count]) {
                return NO;
            }
            return YES;
        }
        else if([self isKindOfClass:[NSDictionary class]]){
            NSDictionary *dic = (NSDictionary *)self;
            if (![dic count]) {
                return NO;
            }
        }
        return YES;
    }
    return YES;
}

/**
 是否是NSDictionary类型
 */
- (BOOL)isDictionaryClass{
    if ([self isKindOfClass:[NSDictionary class]]) return YES;
    return NO;
}

/**
 是否是NSArray类型
 */
- (BOOL)isArrayClass{
    if ([self isKindOfClass:[NSArray class]]) return YES;
    return NO;
}

+ (void)mgc_asyncMainExecuteBlock:(void (^)(void))block {
    if (!block) {
        NSAssert(NO, @"block is nil");
        return;
    }
    if ([NSThread isMainThread]) {
        block();
    }
    else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

@end
